package com.lightkits.wms.controller;

import com.lightkits.framework.common.exception.LightKitsBizException;
import com.lightkits.framework.common.model.IResult;
import com.lightkits.framework.common.utility.object.ObjectUtil;
import com.lightkits.wms.domain.tagservice.UseTagParamsService;
import com.lightkits.wms.dto.tagservice.CreateTagRequestType;
import com.lightkits.wms.dto.tagservice.CreateTagResponseType;
import com.lightkits.wms.dto.tagservice.model.TagViewDto;
import io.choerodon.swagger.annotation.Permission;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * TagServiceController
 *
 * @author jing.wei@hand-china.com
 * Created on 2019/5/22 19:23
 */
@RestController
@RequestMapping("/TagService")
public class TagServiceController {

    @Autowired
    private UseTagParamsService useTagParamsService;

    @PostMapping(value = "/createTag")
    @Permission(permissionPublic = true)
    @ApiOperation(value = "/createTag")
    @ResponseBody
    public CreateTagResponseType createTag(@RequestBody(required = false) CreateTagRequestType request) {
        CreateTagResponseType response = new CreateTagResponseType();
        response.setSuccessful(true);
        try {
            List<TagViewDto> tagViewDtos=new ArrayList<>();
            tagViewDtos.add(request.getViewDTO());
            IResult result = useTagParamsService.insertValue(tagViewDtos);
            if (!result.isSuccessful()) {
                return result.fill(response);
            }

        } catch (LightKitsBizException e) {
            response.setSuccessful(false);
            String mes= StringUtils.substringAfterLast(e.getMessage(),":");
            response.setMessage(ObjectUtil.isNullOrEmpty(mes)?e.getMessage():mes);
        } catch (Exception e) {
            e.printStackTrace();
            response.setSuccessful(false);
            response.setMessage(e.getMessage());
        }
        return response;
    }

}
